using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Possan.Graphics.Filters
{
	/// <summary>
	/// Change contrast and/or brightess of an image
	/// </summary>
	public class ContrastFilter : IBaseFilter
	{
		public string ParameterDefinition
		{
			get
			{
				     return null;
			}
		}


		private int _bri;
		/// <summary>
		/// Brightness adjustment
		/// Range: -100 to +100
		/// -100 = All black
		/// 0 = As input
		/// 100 = All white
		/// </summary>
		public int Brightness
		{
			get
			{
				     return _bri;
			}
			set
			{
				_bri = value;
			}
		}

		private int _con;

		/// <summary>
		/// Contrast adjustment
		/// 
		/// Range: 0+
		/// 0 = No contrast at all
		/// 50 = Half contrast
		/// 100 = As input
		/// 200 = Double contrast
		/// </summary>
		public int Contrast
		{
			get
			{
				     return _con;
			}
			set
			{
				_con = value;
			}
		}

		public ContrastFilter()
		{
			Brightness = 0;
			Contrast = 100;
		}

							 
		public Bitmap Filter(Bitmap bmp)
		{
			Bitmap neu = bmp.Clone() as Bitmap;
			//	neu.SetResolution(inputbitmap.HorizontalResolution, inputbitmap.VerticalResolution);

			float con = (float)Contrast / 100.0f;
			float bri = (float)Brightness / 100.0f;

			for (int j = 0; j < neu.Height ;  j++)
			{
				for (int i = 0; i < neu.Width ;  i++)
				{
					Color c = neu.GetPixel(i, j);

					float fr = (((((float)c.R / 128.0f) - 1.0f) * con) + bri + 1.0f) * 128.0f;
					float fg = (((((float)c.G / 128.0f) - 1.0f) * con) + bri + 1.0f) * 128.0f;
					float fb = (((((float)c.B / 128.0f) - 1.0f) * con) + bri + 1.0f) * 128.0f;

					int ir = (int)Math.Max(Math.Min(fr, 255.0f), 0.0f);
					int ig = (int)Math.Max(Math.Min(fg, 255.0f), 0.0f);
					int ib = (int)Math.Max(Math.Min(fb, 255.0f), 0.0f);

					Color c2 = Color.FromArgb(c.A, ir, ig, ib);
					neu.SetPixel(i, j, c2);
				}
			}

			     return neu;
		}
				    
	}

}





